package top.yiwyn.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.yiwyn.common.ResultBean;
import top.yiwyn.domain.User;
import top.yiwyn.domain.UserLoginParam;
import top.yiwyn.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;


    /**
     * 前台发送请求头应该未 "tokenHeader":"tokenHead token"
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/login")
    public ResultBean login(UserLoginParam user, HttpServletRequest request) {

        return userService.login(user.getUsername(), user.getPassword(), request);
    }

    @PostMapping("/logout")
    public ResultBean logout() {
        return ResultBean.succ("注销成功");
    }

    @GetMapping("/getinfo")
    public ResultBean getInfo(Principal principal) {
        if (principal == null) {
            return null;
        }
        String username = principal.getName();

        return ResultBean.succ("获取成功", username);
    }


}
